package com.qf.proxy.util.http.ge.httpclient;

import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CookieStore;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.BasicCredentialsProvider;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author claud
 * 2018年8月15日17:47:52
 * httpclient的所需参数类
 */
@Data
@Accessors(chain = true)
public class HttpClientConfigZhang {

    /**
     * Post请求参数
     */
    List<NameValuePair> nvpsList = new ArrayList<>();
    /**
     * Post请求字符串
     */
    String postDate;
    /**
     * 请求地址
     */
    private String url = "";
    /**
     * 默认浏览器头
     */
    private String userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.9 Safari/537.36";
    /**
     *
     */
    private String Connection = "keep-alive";
    /**
     *
     */
    private String acceptLanguage = "zh-CN,zh;q=0.9,en-GB;q=0.8,en;q=0.7";
    /**
     *
     */
    private String cacheContro = "no-cache";
    /**
     *
     */
    private String accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
    /**
     *
     */
    private String acceptEncoding = "gzip, deflate, br";
    /**
     *
     */
    private String contentType = "application/x-www-form-urlencoded";
    /**
     * 默认超时时间
     */
    private Integer timeOut = 60000;
    /**
     * 默认代理IP地址
     */
    private String proxyIp = "";
    /**
     * 默认代理IP端口
     */
    private Integer proxyPort = 0;
    /**
     * 默认代理账号
     */
    private String proxyUser = "";
    /**
     * 默认代理密码
     */
    private String proxyPass = "";
    /**
     * 默认Referer地址
     */
    private String referer = "";
    /**
     * 默认浏览器Resquest参数
     */
    private Map<String, String> headerMap = new HashMap<>();
    /**
     * 默认是否自动跳转
     */
    private Boolean autoRedirect = true;
    /**
     * 默认出口IP
     */
    private String localIp = "";
    /**
     * 默认存储路径
     */
    private String savePath = "";
    /**
     * 默认保存文件名
     */
    private String fileName = "";
    /**
     * 默认发送Cookie
     */
    private String cookie = "";
    /**
     * 默认允许跳转次数
     */
    private Integer maxRedirects = 50;

    /**
     * 默认Cookie解析形式
     */
    private String cookieSpecs = "default";

    /**
     * 请求配置
     */
    private RequestConfig requestConfig;

    /**
     * 允许原地址重定向
     */
    private Boolean circularRedirect = false;

    /**
     *
     */
    private CookieStore cookieStore = new BasicCookieStore();

    /**
     * 代理请求配置
     */
    private CredentialsProvider credsProvider;

    public HttpClientConfigZhang build() {

        InetAddress adds = null;
        if (StrUtil.isNotEmpty(localIp)) {
            try {
                adds = InetAddress.getByName(localIp);
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }

        HttpHost proxy = null;
        if (StrUtil.isNotEmpty(proxyIp) && proxyPort > 0) {
            proxy = new HttpHost(proxyIp, proxyPort);
        }

        if (StrUtil.isNotEmpty(proxyIp) && proxyPort > 0 && StrUtil.isNotEmpty(proxyUser) && StrUtil.isNotEmpty(proxyPass)) {
            credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(new AuthScope(proxyIp, proxyPort), new UsernamePasswordCredentials(proxyUser, proxyPass));
        }

        requestConfig = RequestConfig.custom()
                .setCookieSpec(cookieSpecs)
                .setSocketTimeout(timeOut)
                .setConnectTimeout(timeOut)
                .setConnectionRequestTimeout(timeOut)
                .setRedirectsEnabled(autoRedirect)
                .setCircularRedirectsAllowed(circularRedirect)
                .setProxy(proxy)
                .setLocalAddress(adds)
                .setMaxRedirects(maxRedirects)
                .build();

        return this;
    }


}
